package io.nobug.util;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

/**
 * 编写脚本所常用的工具类
 */
public class ActorUtils {
    private ActorUtils() {

    }

    /**
     * 单击鼠标连续获取多个屏幕坐标，按Esc可停止
     */
    public static void getPoints() {
        System.out.println("单击鼠标连续获取多个屏幕坐标，按Esc可停止");
        Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();
        Frame f = new Frame();
        f.setBounds(0, 0, (int) scrSize.getWidth(), (int) scrSize.getHeight());//设置窗口的大小 位置
        f.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                Point point = MouseInfo.getPointerInfo().getLocation();
                System.out.println("(" + (int) point.getX() + ", " + (int) point.getY() + ")");
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });
        f.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {

            }

            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
                    System.exit(0);
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {

            }
        });
        f.setUndecorated(true);
        f.setVisible(true);
        f.setOpacity(0.3F);
    }

    /**
     * 将文本存入剪切板
     *
     * @param text 文本
     * @return 文本
     */
    public static String setClipboard(String text) {
        Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
        clip.setContents(new StringSelection(text), null);
        return text;
    }

    /**
     * 获取剪切板内容
     *
     * @return 剪切板中的文本
     */
    public static String getClipboard() {
        String text = "";
        Clipboard sysClip = Toolkit.getDefaultToolkit().getSystemClipboard();
        Transferable clipTf = sysClip.getContents(null);
        if (clipTf != null) {
            if (clipTf.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                try {
                    text = (String) clipTf.getTransferData(DataFlavor.stringFlavor);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return text;
    }
}
